#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1000005;
const int mod = 520817;
const int seed = 131;
long long fa[maxn],t,n;
struct node{
    long long a,b;
}bd[maxn];

int find(int a){
    if(fa[a]==a)return a;
    else return fa[a]=find(fa[a]);
}

void unionn(int a,int b){
    long long x1 = find(a);
    long long x2 = find(b);
    if(x1!=x2){
        fa[x1]=x2;
    }
    return;
}

int main(){
    cin>>t;
    while(t--){
        for(int i=1;i<=mod;i++){
            fa[i]=i;
        }
        cin>>n;
        long long a,b,c,cnt=0;
        for(int i=1;i<=n;i++){
            cin>>a>>b>>c;
            a=(a*seed)%mod;
            b=(b*seed)%mod;
            if(c==1){
                unionn(a,b);
            }else{
                bd[++cnt].a=a;
                bd[cnt].b=b;
            }
        }
        bool f = true;
        for(int i=1;i<=cnt;i++){
            if(find(bd[i].a)==find(bd[i].b)){
                f=false;
            }
        }
        if(f){
            cout<<"YES"<<endl;
        } else{
            cout<<"NO"<<endl;
        }
    }
}
